/*
Copyright (c) 2009-2010, Warwick Warp Limited
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Warwick Warp Limited nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL WARWICK WARP LIMITED BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef WARRAY_WASSERT_H_INCLUDED
#define WARRAY_WASSERT_H_INCLUDED

//#include <boost/static_assert.hpp>
#include <stdexcept>
#include <sstream>
#include <functional>
#include <csignal>

namespace warp {
	
template<typename T>
std::ostream& operator<<(std::ostream& s, std::equal_to<T>) {return s << "==";}

template<typename T>
std::ostream& operator<<(std::ostream& s, std::not_equal_to<T>) {return s << "!=";}

template<typename T>
std::ostream& operator<<(std::ostream& s, std::greater<T>) {return s << ">";}

template<typename T>
std::ostream& operator<<(std::ostream& s, std::greater_equal<T>) {return s << ">=";}

template<typename T>
std::ostream& operator<<(std::ostream& s, std::less<T>) {return s << "<";}

template<typename T>
std::ostream& operator<<(std::ostream& s, std::less_equal<T>) {return s << "<=";}

struct assert_failed : public std::logic_error
{
	explicit assert_failed(const std::string& what_) :
		std::logic_error(what_)
	{
#ifndef NDEBUG // not not debug!
		// This generates a breakpoint in the debugger, but should continue if
		// one isn't attached
		typedef void (*signal_handler)(int);
		signal_handler old = std::signal(SIGINT,SIG_IGN);
		std::raise(SIGINT);
		std::signal(SIGINT,old);
#endif
	}
};

inline void assert_true( const char *file, int line, 
	const char *s, bool condition )
{
	if(!condition)
	{
		std::ostringstream fmt;
		fmt << file << ":" << line 
			<< " Assertion " << s << " failed.";
		throw assert_failed(fmt.str());
	}
}

template<typename T, typename COMPARE>
void assert_compare( const char *file, int line, 
	const char *xs, const char *ys, T x, T y, COMPARE comp)
{
	if(!comp(x,y))
	{
		std::ostringstream fmt;
		fmt << file << ':' << line << ' '
			<< "Assertion " << xs << ' ' << comp << ' ' << ys << " failed "
			<< "![" << x << ' ' << comp << ' ' << y << "].";
		throw assert_failed(fmt.str());
	}
}

template<typename T>
void assert_equal( const char *file, int line, 
	const char *xs, const char *ys, T x, T y)
{
	assert_compare(file,line,xs,ys,x,y,std::equal_to<T>());
}

template<typename T>
void assert_equal( const char *file, int line, 
	const char *xs, const char *ys, const char *zs, T x, T y, T z)
{
	assert_compare(file,line,xs,ys,x,y,std::equal_to<T>());
	assert_compare(file,line,xs,zs,x,z,std::equal_to<T>());
}

template<typename T>
void assert_ne( const char *file, int line, 
	const char *xs, const char *ys, T x, T y)
{
	assert_compare(file,line,xs,ys,x,y,std::not_equal_to<T>());
}

template<typename T>
void assert_ge( const char *file, int line, 
	const char *xs, const char *ys, T x, T y)
{
	assert_compare(file,line,xs,ys,x,y,std::greater_equal<T>());
}

template<typename T>
void assert_gt( const char *file, int line, 
	const char *xs, const char *ys, T x, T y)
{
	assert_compare(file,line,xs,ys,x,y,std::greater<T>());
}

template<typename T>
void assert_le( const char *file, int line, 
	const char *xs, const char *ys, T x, T y)
{
	assert_compare(file,line,xs,ys,x,y,std::less_equal<T>());
}

template<typename T>
void assert_lt( const char *file, int line, 
	const char *xs, const char *ys, T x, T y)
{
	assert_compare(file,line,xs,ys,x,y,std::less<T>());
}
	
template<typename T>
void assert_range( const char *file, int line, 
	const char *lowers, const char *xs, const char* uppers, T l, T x, T u)
{
	assert_le(file,line,lowers,xs,l,x);
	assert_lt(file,line,xs,uppers,x,u);
}

} // namespace warp

// Macros for embedding file/line (that are compiled out)
#ifdef NDEBUG
#define warp_assert_true( condition ) void(0)
#define warp_assert_equal( x, y )     void(0)
#define warp_assert_equal3( x, y, z ) void(0)
#define warp_assert_ge( x, y )        void(0)
#define warp_assert_gt( x, y )        void(0)
#define warp_assert_le( x, y )        void(0)
#define warp_assert_lt( x, y )        void(0)
#define warp_assert_ne( x, y )        void(0) 
#define warp_assert_range( l, x, u )  void(0) 
#else
#define warp_assert_true( condition ) warp::assert_true(__FILE__,__LINE__,#condition,(condition))
#define warp_assert_equal( x, y )     warp::assert_equal(__FILE__,__LINE__,#x,#y,x,y)
#define warp_assert_equal3( x, y, z ) warp::assert_equal(__FILE__,__LINE__,#x,#y,#z,x,y,z)
#define warp_assert_ge( x, y )        warp::assert_ge(__FILE__,__LINE__,#x,#y,x,y)
#define warp_assert_gt( x, y )        warp::assert_gt(__FILE__,__LINE__,#x,#y,x,y)
#define warp_assert_le( x, y )        warp::assert_le(__FILE__,__LINE__,#x,#y,x,y)
#define warp_assert_lt( x, y )        warp::assert_lt(__FILE__,__LINE__,#x,#y,x,y)
#define warp_assert_ne( x, y )        warp::assert_ne(__FILE__,__LINE__,#x,#y,x,y)
#define warp_assert_range( l, x, u )  warp::assert_range(__FILE__,__LINE__,#l,#x,#u,l,x,u)
#endif

// Macros for embedding file/line (that are not compiled out)
#define warp_verify_true( condition ) warp::assert_true(__FILE__,__LINE__,#condition,(condition))
#define warp_verify_equal( x, y )     warp::assert_equal(__FILE__,__LINE__,#x,#y,x,y)
#define warp_verify_equal3( x, y, z ) warp::assert_equal(__FILE__,__LINE__,#x,#y,#z,x,y,z)
#define warp_verify_ge( x, y )        warp::assert_ge(__FILE__,__LINE__,#x,#y,x,y)
#define warp_verify_gt( x, y )        warp::assert_gt(__FILE__,__LINE__,#x,#y,x,y)
#define warp_verify_le( x, y )        warp::assert_le(__FILE__,__LINE__,#x,#y,x,y)
#define warp_verify_lt( x, y )        warp::assert_lt(__FILE__,__LINE__,#x,#y,x,y)
#define warp_verify_ne( x, y )        warp::assert_ne(__FILE__,__LINE__,#x,#y,x,y)
#define warp_verify_range( l, x, u )  warp::assert_range(__FILE__,__LINE__,#l,#x,#u,l,x,u)
#endif
